<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-09 22:00:52
 * @LastEditors: Sam
 * @LastEditTime: 2022-03-09 17:34:10
 */
namespace app\api\controller;
use think\facade\Db;
use app\__MODULE__\model\{Notice as NoticeModel,NoticeType,NoticeRead};
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("资讯通知")
 */
class Notice extends Base {
    

    /**
     * @Apidoc\Title("公告分类")
     * @Apidoc\Url("/api/notice_type")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("[]", type="list", 
     *      @Apidoc\Returned("id", type="int", desc="公告id"),
     *      @Apidoc\Returned("title", type="string", desc="公告标题")
     * )
     */
    public function type(){
        $list = NoticeType::field('id,title')->order('sort desc,id desc')->select();
        json_response(1,'success',$list);
    }

    /**
     * @Apidoc\Title("全部公告")
     * @Apidoc\Url("/api/notice_index")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("type", type="int", desc="公告分类id"),
     * @Apidoc\Param("page", type="int", desc="页码",default=1),
     * @Apidoc\Param("limit", type="int", desc="数据量",default=10),
     * @Apidoc\Param("keyword", type="strinng", desc="关键字搜索"),
     * @Apidoc\Returned("[]", type="list", 
     *      @Apidoc\Returned("id", type="int", desc="公告id"),
     *      @Apidoc\Returned("title", type="string", desc="公告标题"),
     *      @Apidoc\Returned("thumb", type="string", desc="图片"),
     *      @Apidoc\Returned("sub_title", type="string", desc="副标题"),
     *      @Apidoc\Returned("create_time", type="string", desc="发布时间"),
     *      @Apidoc\Returned("is_read", type="int", desc="是否已读 "),
     * )
     */
    public function index(){
    	$type  = request()->param('type/d',0);
        $page  = \request()->param('page/d',1);
        $limit = \request()->param('limit/d',10);
        $keyword = \request()->param('keyword/s');
        $uid   = self::get_user_id();
        $where = [];
        $where[]= ['is_show','=',1];
        if($type){
            $where[] = ['type','=',$type];
        }
        if($keyword){
            $where[] = ['title','like',"%{$keyword}%"];
        }
        $list = NoticeModel::where($where)->field('id,title,thumb,sub_title,create_time,type')->order('is_top desc,sort desc')->page($page,$limit)->select();
        if(count($list)){
            foreach ($list as $key => &$value) {
                $value['is_read'] = NoticeRead::where('uid',$uid)->where('nid',$value['id'])->count() ? true : false;
            }
        }
        
        json_response(1,'success',$list);
    }

    /**
     * @Apidoc\Title("公告详情")
     * @Apidoc\Url("/api/notice_detail")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("id", type="int",require=true, desc="公告id"),
     * @Apidoc\Returned("title", type="string", desc="公告标题"),
     * @Apidoc\Returned("thumb", type="string", desc="图片"),
     * @Apidoc\Returned("sub_title", type="string", desc="副标题"),
     * @Apidoc\Returned("create_time", type="string", desc="发布时间"),
     * @Apidoc\Returned("detail", type="text", desc="详情 "),
     * @Apidoc\Returned("type_text", type="string", desc="分类名称 "),
     */
    public function detail(){
    	$id = param_check('id');
        $uid = self::get_user_id();
    	$detail = NoticeModel::with('type')->where('id',$id)->field('id,title,type,thumb,sub_title,create_time,type,detail')->find();
    	if(!$detail) json_response(0,'公告不存在');
        if(!NoticeRead::where('uid',$uid)->where('nid',$id)->count()){
            NoticeRead::create([
                'uid' => $uid,
                'nid' => $id
            ]);
        }
    	json_response(1,'success',$detail);
    }
}

